package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Grades;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.GroupInfo;
import com.eece496.Helpers.StudentInfo;
import com.eece496.Helpers.TADutiesInfo;

/**
 * Servlet implementation class TAMakeAssessmentsServlet
 */
public class TAMakeAssessmentsServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TAMakeAssessmentsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String bigString = request.getParameter("courseNumber");
		System.out.println(bigString);
		
		String division = bigString.substring(0,1);
		String courseNumber = bigString.substring(2,10);
		String sectionNumber= bigString.substring(11,14);
		String TAID = bigString.substring(15,23);
		String COGSName = bigString.substring(24,(bigString.length()));
		String button = request.getParameter("button");
		request.setAttribute("viewButton", button);
		
		System.out.println("Division: " + division);
		System.out.println("CourseNumber: " + courseNumber);
		System.out.println("SectionNumber: " + sectionNumber);
		System.out.println("TA ID: " + TAID);
		System.out.println("COGS Name: " + COGSName);
		
		
		int tID = Integer.parseInt(TAID);
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String empty = request.getParameter("empty");
		
		ArrayList<TADutiesInfo> info = new ArrayList<TADutiesInfo>();
		TAduties TAdutiesDB = new TAduties();
		TAdutiesDB.Setup(connection);
		
		//Create an ArrayList and add all those student numbers
		ArrayList<ArrayList<GroupInfo>> studentList = new ArrayList<ArrayList<GroupInfo>>();
		//Need to also create an ArrayList of student groups
		ArrayList<GroupInfo> groupList  = new ArrayList<GroupInfo>();
		
		ArrayList<String> theList = new ArrayList<String>();
		
		// Get the group number from TA duties
		try {
			theList = TAdutiesDB.GetGroupNumbers(tID, COGSName, courseNumber, sectionNumber, division);
			for(String str: theList)
			{
				int groupNum = Integer.parseInt(str);
				int groupNumNext = 0;
				//check if group Number is odd or even
				//even
				if(groupNum%2 == 0)
				{
					groupNumNext = groupNum - 1;
				}
				//odd
				else
				{
					groupNumNext = groupNum + 1;
				}
			
				System.out.println("the First Group Number: " + groupNum);
				System.out.println("the Section Group Number: " + groupNumNext);
				//Get the student info from each group and add to ArrayList of type studentinfo
				StudentGroups groupsDB = new StudentGroups();
				groupsDB.Setup(connection);
				groupList = groupsDB.GetTwoStudentGroupInfo(courseNumber, sectionNumber, groupNum, groupNumNext, division);
				studentList.add(groupList);
				
			}
			
			System.out.println(studentList.size());
			request.setAttribute("studentList", studentList);
			
		//From the arraylist get the student info  and add to another arraylist
			info = TAdutiesDB.GetStudentList(tID, courseNumber, sectionNumber, COGSName);
			
			//Get the list of Individual Grades for the Students
			Grades GradesDB = new Grades();
			GradesDB.Setup(connection);
			ArrayList<String> IndGrades = new ArrayList<String>();
			for(TADutiesInfo Tinfo: info){
				String indGrade = GradesDB.GetGradeFromStudent(Tinfo.studentID, COGSName, courseNumber, sectionNumber, "individual", "Ind");
				IndGrades.add(indGrade);
			}
			request.setAttribute("IndGrades", IndGrades);
		
			//Get the list of Group Grades for the Students;
			ArrayList<String> GroupGrades = new ArrayList<String>();
			int count = 1;
			for(TADutiesInfo Tinfo: info){
				String groupGrade = GradesDB.GetGradeFromStudent(Tinfo.studentID, COGSName, courseNumber, sectionNumber, "group", "Group"+count);
				GroupGrades.add(groupGrade);
				count++;
			}
			request.setAttribute("GroupGrades", GroupGrades);
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		request.setAttribute("division", division);
		request.setAttribute("TADutyInfo", info);
		request.setAttribute("courseNumber", courseNumber);
		request.setAttribute("sectionNumber", sectionNumber);
		request.setAttribute("TAID", TAID);
		request.setAttribute("COGSName", COGSName);
		request.setAttribute("firstname", firstname);
		request.setAttribute("lastname", lastname);
		request.setAttribute("empty", empty);
		
		request.getRequestDispatcher("/TA/TA-Make-Assessments.jsp")
		.forward(request, response);
	}
}